﻿using System.Windows;
using System.Windows.Controls;
using System.IO;
using Jerry.DroidBuilder.ThemeEditor.Controls;
using System;

namespace Jerry.DroidBuilder.ThemeEditor
{
    public class DrawableContentTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var control = container as FrameworkElement;

            ComponentResourceKey componentKey = null;

            if (item is string)
            {
                componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_ColorDrawableTemplate");
            }
            else if (item is DrawableInfo)
            {
                var info = item as DrawableInfo;

                if (info.IsComposited && info.IsPatched)
                {
                    componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_CompositePatchedDrawableTemplate");
                }
                else if (info.IsComposited)
                {
                    componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_CompositeDrawableTemplate");
                }
                else if (info.IsPatched)
                {
                    componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_PatchedDrawableTemplate");
                }
                else
                {
                    componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_ImageDrawableTemplate");
                }
            }
            else
            {
                componentKey = new ComponentResourceKey(typeof(LayoutElement), "Missing");
            }

            //if (val.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase))
            //{
            //    componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_CompositeDrawableTemplate");
            //}
            //else if (val.EndsWith("9.png", StringComparison.InvariantCultureIgnoreCase))
            //{
            //    componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_NinePatchDrawableTemplate");
            //}
            //else if (val.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)
            //    || val.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase))
            //{
            //    componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_ImageDrawableTemplate");
            //}
            //else if (val.StartsWith("#"))
            //{
            //    componentKey = new ComponentResourceKey(typeof(LayoutElement), "Template_ColorDrawableTemplate");
            //}
            //else
            //{
            //    componentKey = new ComponentResourceKey(typeof(LayoutElement), "Missing");
            //}

            var resource = control.TryFindResource(componentKey) as DataTemplate;

            if (null == resource)
            {
                resource = base.SelectTemplate(item, container);
            }

            return resource;
        }
    }
}
